// pages/become_leader/Bbecome_leader.js
//手动实现sleep
var tools = require('../../js/tools.js')
const app = getApp()

function sleep(d) {
  for (var t = Date.now(); Date.now() - t <= d;);
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ifpublish: false,//用它来指定是否为管理员
    publishloading: false,//发布的loading图标是否显示
    basics: 0,
    numList: [{
      name: '输入组名'
    }, {
      name: '完成创建'
    }, {
      name: '邀请加入'
    },],
    num: 0,
    scroll: 1,
    value: '',
    ifloading: true,
    groupID: '000000',
    ifgroupID: false,
    ifecode: true,
    ifnext: true
  },

  nextSteps: function () {
    var that = this
    if (this.data.num != 2 && this.data.value != '') {
      this.setData({
        num: this.data.num += 1
      })
    }
    if (this.data.value == '') {
      wx.showToast({
        title: '名字不能为空',
        image: '../../pages/Personal_info/err.png',
        duration: 600
      })
    }
    if (this.data.num == 1) {
      wx.getStorage({
        key: 'pwdWithSalt',
        success(res) {
          wx.request({
            url: 'https://localhost:8080/createGroup/',
            data: {
              pwdWithSalt: res.data,
              name: that.data.value
            },
            success(res) {
              if (res.data == -1) {
                wx.showToast({
                  title: '网络异常',
                  icon: '../../pages/Personal_info/err.png',
                  duration: 1000,
                })
              }
              else {
                that.setData({
                  ifgroupID: true,
                  groupID: res.data,
                  ifloading: false,
                  ifecode: true
                })
                wx.request({
                  url: 'https://localhost:8080/getecd',
                  data: {
                    groupID: that.data.groupID
                  },
                  success(res) {
                    that.setData({ ifecode: false, imgurl: "data:image/PNG;base64," + res.data })
                  }
                })
              }

            }
          })
        },
        fail(res) {
          console.log(res)
        }
      });
    }
    if (this.data.num == 2) {
      that.setData({
        ifnext: false
      })
      wx.getStorage({
        key: 'pwdWithSalt',
        success(res) {
          console.log(res.data)
          wx.request({
            url: 'https://localhost:8080/getInfoToLocal/',
            data: {
              pwdWithSalt: res.data
            },
            success(res) {
              app.globalData.admin = res.data.admin
              console.log(res.data.admin)
              //存入本地缓存
              wx.setStorage({
                key: 'admin',
                data: res.data.admin
              })
            }
          })
        }
      })


      wx.showToast({
        title: '创建成功',
        icon: 'success',
        duration: 1000,
      })
      sleep(3000)
      wx.navigateBack({
        delta: 1
      })
    }
  },

  lastSteps: function () {
    if (this.data.num != 0) {
      this.setData({
        num: this.data.num -= 1
      })
    }
  },

  Bpublish: function () {
    var that = this
    wx.vibrateShort()//震动
    this.setData({
      publishloading: true
    })
    wx.navigateTo({
      url: '../../pages/publish/publish',
      success: function () {
        that.setData({
          publishloading: false
        })
      }
    })

  },

  //按钮创建的事件相应函数
  Bcreate: function () {
    this.setData({
      ifpublish: false
    })
  },

  BLsave: function () {
    var imgSrc = this.data.imgurl.slice(22);//base64编码
    var save = wx.getFileSystemManager();
    var number = Math.random();
    save.writeFile({
      filePath: wx.env.USER_DATA_PATH + '/pic' + number + '.png',
      data: imgSrc,
      encoding: 'base64',
      success: res => {
        wx.saveImageToPhotosAlbum({
          filePath: wx.env.USER_DATA_PATH + '/pic' + number + '.png',
          success: function (res) {
            wx.showToast({
              title: '保存成功',
            })
          },
          fail: function (err) {
            console.log(err)
          }
        })
        console.log(res)
      }, fail: err => {
        console.log(err)
      }
    })
  },

  onChange: function (event) {
    //把输入的值存入变量
    this.setData({
      value: event.detail.detail.value
    })
    // var keywords = ["推翻", "中国", "阿賓"];  //敏感词词库
    // // 具体判断方法

    // //获取文本输入框中的内容  
    // var value = this.data.value
    // //遍历敏感词数组  
    // for (var i = 0; i < keywords.length; i++) 
    // {
    //   //全局替换  
    //   var reg = new RegExp(keywords[i], "g");
    //   //判断内容中是否包括敏感词  
    //   if (value.indexOf(keywords[i]) != -1) 
    //   {
    //     var result = value.replace(reg, "**");
    //     value = result;
    //   }
    // }
    // this.setData({
    //   value: value
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getStorage({
      key: 'pwdWithSalt',
      success(res) {
        wx.request({
          url: 'https://localhost:8080/getadmin/',
          data: {
            pwdWithSalt: res.data
          },
          success(res) {
            if (res.data > 0) {
              that.setData({
                ifpublish: true
              })
            }
            if (res.data >= 5) {
              wx.showToast({
                title: '一个人最多创建5个小组',
                icon: 'none',
                duration: 2000,
              })
              sleep(2000)
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.getStorage({
      key: 'Landingtime',
      success(res) {
        // console.log("登录时间信息存在，正在检查是否过期..")
        // tools.gettimestamp()
        var date = tools.convertDateFromString(res.data)
        if (tools.comparetime(date) == 0) {
          wx.navigateBack({
              delta: 1
          });
          //已经过期
          wx.showToast({
            title: '请先点击"我的"里的头像登录',
            icon: 'none',
            duration: 3000
          })
        }
        else{
        }
      },
      fail(res) {//此时为第一次登陆
          wx.navigateBack({
              delta: 1
          });
        wx.showToast({
          title: '请先点击"我的"里的头像登录',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})